import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:get/get.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:wallet_utils/base_router_config.dart';
import 'package:wallet_utils/color.dart';
import 'package:wallet_utils/report_widget/click_widget.dart';
import 'package:wallet_utils/text_style.dart';
import 'package:wallet_utils/webview_common.dart';
import 'logic.dart';

class MinePage extends StatefulWidget {
  @override
  State<MinePage> createState() => _MinePageState();
}

class _MinePageState extends State<MinePage> with AutomaticKeepAliveClientMixin {
  final logic = Get.put(MinePageLogic());
  final state = Get.find<MinePageLogic>().state;

  @override
  Widget build(BuildContext context) {
    return AnnotatedRegion<SystemUiOverlayStyle>(
      value: SystemUiOverlayStyle.dark,
      child: Scaffold(
        backgroundColor: ColorManager.colorF4F5F6,
        bottomNavigationBar: ClickWidget(
          onTap: () {
            contactServiceWidget(context);
          },
          child: Container(
            height: 50.w + ScreenUtil().bottomBarHeight,
            alignment: Alignment.center,
            child: Row(
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                Image.asset(
                  'images/user_service.png',
                  width: 17.w,
                  height: 18.w,
                ),
                SizedBox(
                  width: 5.w,
                ),
                Text(
                  '联系客服',
                  style: TextStyleUtils.style999999(fontSize: 14.sp),
                )
              ],
            ),
          ),
        ),
        body: GetBuilder<MinePageLogic>(builder: (logic) {
          return SingleChildScrollView(
            child: Column(
              children: [
                Stack(
                  children: [
                    Container(
                      height: 263.w,
                      decoration: const BoxDecoration(
                        image: DecorationImage(
                          image: AssetImage("assets/user_header_bg.png"),
                          fit: BoxFit.cover,
                        ),
                      ),
                    ),
                    Visibility(
                      visible: false,
                      child: Positioned(
                          top: ScreenUtil().statusBarHeight + 20.w,
                          right: 16.w,
                          child: Image.asset(
                            'images/user_msg.png',
                            width: 22.w,
                            height: 19.w,
                          )),
                    ),
                    ClickWidget(
                      onTap: () {
                        if (!state.isLogin) {
                          Get.toNamed(BaseRouterConfig.smsPage);
                        }
                      },
                      child: Container(
                        margin:
                            EdgeInsets.only(top: ScreenUtil().statusBarHeight + 34.w, left: 16.w),
                        child: Row(
                          children: [
                            Image.asset(
                              'images/user_avatar.png',
                              width: 56.w,
                              height: 56.w,
                            ),
                            SizedBox(
                              width: 7.w,
                            ),
                            Column(
                              crossAxisAlignment: CrossAxisAlignment.start,
                              children: [
                                Text(
                                  logic.mineInfo?.mobile ?? '未获取手机号码',
                                  style: TextStyleUtils.styleFFFFFF(
                                      fontSize: 18.sp, fontWeight: FontWeight.bold),
                                ),
                                Text(
                                  logic.mineInfo?.userName ?? '放鑫钱包',
                                  style: TextStyleUtils.styleFFFFFF(
                                      fontSize: 14.sp, fontWeight: FontWeight.bold),
                                )
                              ],
                            ),
                          ],
                        ),
                      ),
                    ),
                    Container(
                      height: 87.w,
                      margin: EdgeInsets.only(
                          top: ScreenUtil().statusBarHeight + 160.w, left: 16.w, right: 16.w),
                      decoration: const BoxDecoration(
                        color: Colors.white,
                        //设置四周圆角 角度 这里的角度应该为 父Container height 的一半
                        borderRadius: BorderRadius.all(Radius.circular(10.0)),
                      ),
                      child: Row(
                        mainAxisAlignment: MainAxisAlignment.spaceBetween,
                        children: [
                          mineRepaymentWidget('images/user_repayment.png', '我要还款'),
                          mineRepaymentWidget('images/user_loan_history.png', '贷款记录'),
                        ],
                      ),
                    )
                  ],
                ),
                Visibility(
                  visible: logic.isShow() ? true : false,
                  child: Container(
                    height: 180.w,
                    margin: EdgeInsets.only(left: 16.w, right: 16.w, top: 10.w),
                    decoration: const BoxDecoration(
                      color: Colors.white,
                      //设置四周圆角 角度 这里的角度应该为 父Container height 的一半
                      borderRadius: BorderRadius.all(Radius.circular(10.0)),
                    ),
                    child: Container(
                      margin: const EdgeInsets.all(5),
                      decoration: BoxDecoration(
                        color: ColorManager.colorFFF7F2,
                        //设置四周圆角 角度 这里的角度应该为 父Container height 的一半
                        borderRadius: BorderRadius.all(Radius.circular(10.0)),
                      ),
                      child: Container(
                        margin: EdgeInsets.only(top: 20.w, bottom: 20.w, left: 16.w, right: 16.w),
                        child: Column(
                          crossAxisAlignment: CrossAxisAlignment.start,
                          children: [
                            Row(
                              children: [
                                Image.asset(
                                  'images/user_tag.png',
                                  width: 13.w,
                                  height: 13.w,
                                ),
                                SizedBox(
                                  width: 5.w,
                                ),
                                Text(
                                  logic.homeState.headProductName ?? '',
                                  style: TextStyleUtils.style000000(
                                      fontSize: 14.sp, fontWeight: FontWeight.bold),
                                ),
                                SizedBox(
                                  width: 5.w,
                                ),
                                Text(
                                  '已为超20万人提供服务',
                                  style: TextStyleUtils.style666666(fontSize: 10.sp),
                                ),
                              ],
                            ),
                            Container(
                                margin: EdgeInsets.only(top: 17.w, bottom: 6.w),
                                child: Text(
                                  '最高可获得(元)',
                                  style: TextStyleUtils.style666666(fontSize: 14.sp),
                                )),
                            Row(
                              children: [
                                Expanded(
                                  child: Text(
                                    logic.homeState.headLoanAmount ?? '',
                                    style: TextStyleUtils.style000000(
                                        fontSize: 30.sp, fontWeight: FontWeight.bold),
                                  ),
                                ),
                                ClickWidget(
                                  onTap: () {
                                    logic.homeLogic.jumpPage();
                                    // Get.to(WebViewCommon(
                                    //   state.linkUrl ?? '',
                                    // ));
                                  },
                                  child: Container(
                                    width: 115.w,
                                    height: 35.w,
                                    alignment: const Alignment(0, 0),
                                    decoration: BoxDecoration(
                                      color: ColorManager.colorD93C3A,
                                      //设置四周圆角 角度 这里的角度应该为 父Container height 的一半
                                      borderRadius: BorderRadius.all(const Radius.circular(25.0)),
                                    ),
                                    child: Text(
                                      '领取我的额度',
                                      style: TextStyleUtils.styleFFFFFF(
                                          fontSize: 14.sp, fontWeight: FontWeight.bold),
                                    ),
                                  ),
                                ),
                              ],
                            ),
                            SizedBox(
                              height: 12.w,
                            ),
                            Row(
                              children: [
                                infoWidget('快速放款'),
                                Container(
                                  margin: EdgeInsets.only(left: 5.w, right: 5.w),
                                  child: infoWidget('还款灵活'),
                                ),
                                infoWidget('息费透明'),
                              ],
                            ),
                          ],
                        ),
                      ),
                    ),
                  ),
                ),
                MediaQuery.removePadding(
                    context: context,
                    removeTop: true,
                    child: Container(
                      margin: EdgeInsets.only(top: 10.w, left: 16.w, right: 16.w),
                      decoration: BoxDecoration(
                        color: ColorManager.colorFFFFFF,
                        //设置四周圆角 角度 这里的角度应该为 父Container height 的一半
                        borderRadius: BorderRadius.all(const Radius.circular(25.0)),
                      ),
                      child: ListView.builder(
                        shrinkWrap: true,
                        itemCount: state.list.length,
                        physics: const NeverScrollableScrollPhysics(),
                        itemBuilder: ((context, index) {
                          return mineWidget(index);
                        }),
                      ),
                    )),
              ],
            ),
          );
        }),
      ),
    );
  }

  Future<dynamic> contactServiceWidget(BuildContext context) {
    return showModalBottomSheet(
        context: context,
        isDismissible: true,
        isScrollControlled: false,
        shape: const RoundedRectangleBorder(
            borderRadius:
                BorderRadius.only(topLeft: Radius.circular(15), topRight: Radius.circular(15))),
        builder: (BuildContext buildContext) {
          return Container(
            height: 150.w,
            decoration: const BoxDecoration(
                color: Colors.white,
                borderRadius:
                    BorderRadius.only(topLeft: Radius.circular(15), topRight: Radius.circular(15))),
            child: Column(
              children: [
                SizedBox(
                  height: 15.w,
                ),
                Text(
                  '请选择联系方式',
                  style: TextStyleUtils.style000000(fontSize: 16.sp, fontWeight: FontWeight.bold),
                ),
                SizedBox(
                  height: 20.w,
                ),
                ClickWidget(
                  onTap: () {
                    logic.contact('拨打电话');
                  },
                  child: Text(
                    '拨打电话',
                    style: TextStyleUtils.style000000(fontSize: 16.sp, fontWeight: FontWeight.bold),
                  ),
                ),
                SizedBox(
                  height: 20.w,
                ),
                ClickWidget(
                  onTap: () {
                    logic.contact('联系客服');
                  },
                  child: Text(
                    '联系客服',
                    style: TextStyleUtils.style000000(fontSize: 16.sp, fontWeight: FontWeight.bold),
                  ),
                ),
              ],
            ),
          );
        });
  }

  Expanded mineRepaymentWidget(String image, String title) {
    return Expanded(
      child: ClickWidget(
        onTap: () {
          logic.payInfo(title);
        },
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            Image.asset(
              image,
              width: 28.w,
              height: 23.w,
            ),
            SizedBox(
              height: 10.w,
            ),
            Text(
              title,
              style: TextStyleUtils.style000000(fontSize: 14.sp),
            )
          ],
        ),
      ),
    );
  }

  Widget mineWidget(int index) {
    return ClickWidget(
      onTap: () {
        logic.mineCheck(index);
      },
      child: Column(
        children: [
          Container(
            height: 65.w,
            margin: EdgeInsets.only(left: 20.w, right: 20.w),
            child: Row(
              children: [
                Image.asset(
                  state.list[index].image ?? '',
                  width: 25.w,
                  height: 25.w,
                ),
                SizedBox(
                  width: 12.w,
                ),
                Expanded(
                    child: Text(
                  state.list[index].title ?? '',
                  style: TextStyleUtils.style000000(fontSize: 16.sp),
                )),
                Image.asset(
                  'images/user_arrow.png',
                  width: 13.w,
                  height: 13.w,
                ),
              ],
            ),
          ),
          Container(
            height: 0.5.w,
            margin: EdgeInsets.only(left: 20.w, right: 20.w),
            color: ColorManager.colorECECEC,
          )
        ],
      ),
    );
  }

  Widget infoWidget(String tag) {
    return Container(
      width: 52.w,
      height: 20.w,
      alignment: Alignment.center,
      decoration: BoxDecoration(
        color: ColorManager.colorFFECE9,
        //设置四周圆角 角度 这里的角度应该为 父Container height 的一半
        borderRadius: BorderRadius.all(Radius.circular(2.0)),
      ),
      child: Text(
        tag,
        style: TextStyleUtils.styleFFFFFF(fontSize: 10.sp),
      ),
    );
  }

  @override
  bool get wantKeepAlive => true;
}
